package models;
import java.util.*;


// <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
// #[regen=yes,id=DCE.F8EF2DB5-BB16-A4BA-7255-98F2C9E6DB11]
// </editor-fold> 
public class ListContacts extends ArrayList<Contact> {

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.58CEC34A-B6BE-235C-A141-BD4BB60707B3]
    // </editor-fold> 
    public ListContacts () {
        super(10); // 10 contacts for a small local network
    }

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.DD0FE637-47FA-7B7E-A462-8DFD6E82E3E4]
    // </editor-fold>
    /**
     * Add a contact to the list
     * @param contact
     * @return true
     */
    public boolean addContact (Contact contact) {
        return super.add(contact);
    }

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.C586DE2B-4B51-5AB6-F0E9-750C78775BE5]
    // </editor-fold>
    /**
     * Remove a contact from the list
     * @param contact 
     * @return true if the list contains the contact
     */
    public boolean removeContact (Contact contact) {
        return super.remove(contact);
    }

    /**
     * Remove a contact given its nickname
     * @param nickname is a String
     * @return true if the list contains the contact with the nickname
     */
    public boolean removeContact (String nickname) {
        boolean foundIt = false;
        Contact contact;
        for (Iterator<Contact> it = this.iterator(); !foundIt && it.hasNext();) {
            contact = it.next();
            if (contact.getNickname() == null ? nickname == null : contact.getNickname().equals(nickname)){
                foundIt = true;
                removeContact(contact);
            }
        }
        return foundIt;
    }

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.71A99237-A426-D5B6-F923-10B3767652BB]
    // </editor-fold>
    /**
     * Remove all the contacts in the list
     * @return true
     */
    public boolean removeAllContacts () {
        return super.removeAll(this);
    }

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.18A5BD84-D919-15DB-52FA-DFACA2148545]
    // </editor-fold>
    /**
     * Get a contact from the list given its nickname
     * @param nickname is a String
     * @return the contact with the nickname passed in or null if the list doesnt contain the contact
     */
    public Contact getContact (String nickname) {
        boolean foundIt = false;
        Contact contact = null;
        for (Iterator<Contact> it = this.iterator(); !foundIt && it.hasNext();) {
            contact = it.next();
            if (contact.getNickname() == null ? nickname == null : contact.getNickname().equals(nickname)){
                foundIt = true;
            }
        }
        if (!foundIt)
            contact = null;
        return contact;
    }

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.7369A8A5-CDD6-00C8-BD4C-4983B76E681B]
    // </editor-fold>
    /**
     * Get a contact from the list given its address
     * @param address is a String
     * @return the contact with the address passed in or null if the list doesnt contain the contact
     */
    public Contact getContactByAddress (String address) {
        boolean foundIt = false;
        Contact contact = null;
        for (Iterator<Contact> it = this.iterator(); !foundIt && it.hasNext();) {
            contact = it.next();
            if (contact.getAddress() == null ? address == null : contact.getAddress().equals(address)){
                foundIt = true;
            }
        }
        if (!foundIt)
            contact = null;
        return contact;
    }

    @Override
    public String toString(){
        Contact contact = null;
        String list = "";
        for (Iterator<Contact> it = this.iterator(); it.hasNext();) {
            contact = it.next();
            list = list + contact.toString() + "\n";
        }
        return list;
    }

    public String[] toStringTable() {
        String[] list = new String[this.size()];
        int i = 0;
        for (Iterator<Contact> it = this.iterator(); it.hasNext();) {
            Contact contact = it.next();
            list[i] = contact.getNickname();
            i++;
        }
        return list;
    }

}

